package net.designbased.jspfire;

import java.util.ArrayList;
import java.io.StringWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.*;
import com.thoughtworks.xstream.io.xml.XppDomDriver;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import net.designbased.jspfire.util.Utility;

/**
 * 图表类
 * @author chenhongliang
 */
@XStreamAlias("vc:Chart")
public class Chart {

    @XStreamAlias("xmlns:vc")
    @XStreamAsAttribute
    private String xmlns = "clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts";
    @XStreamOmitField
    private String id;
    @XStreamOmitField
    private String xapDir;
    @XStreamOmitField
    private String jsDir;
    @XStreamAlias("View3D")
    @XStreamAsAttribute
    private boolean view3D = false;
    @XStreamAlias("AnimationEnabled")
    @XStreamAsAttribute
    private boolean animationEnabled = false;
    @XStreamAlias("Width")
    @XStreamAsAttribute
    private Double width;
    @XStreamAlias("Height")
    @XStreamAsAttribute
    private Double height;
    @XStreamAlias("Theme")
    @XStreamAsAttribute
    private String theme;
    @XStreamAlias("ToolBarEnabled")
    @XStreamAsAttribute
    private boolean toolBarEnabled = false;
    @XStreamAlias("Bevel")
    @XStreamAsAttribute
    private boolean bevel = false;
    @XStreamAlias("LightingEnabled")
    @XStreamAsAttribute
    private boolean lightingEnabled = false;
    @XStreamAlias("ToolTipEnabled")
    @XStreamAsAttribute
    private boolean toolTipEnabled = true;
    @XStreamAlias("vc:Chart.AxesX")
    private Axis[] axesX;
    @XStreamAlias("vc:Chart.AxesY")
    private Axis[] axesY;
    @XStreamAlias("vc:Chart.Series")
    private ArrayList<DataSeries> series = new ArrayList<DataSeries>();
    @XStreamAlias("vc:Chart.Titles")
    private ArrayList<Title> titles;

    /**
     * 默认构造函数
     */
    public Chart() {
        this.id = java.util.UUID.randomUUID().toString();
    }

    /**
     * 带xap包路径和js路径的构造函数
     * @param xapDir
     * @param jsDir
     */
    public Chart(String xapDir, String jsDir) {
        this.id = java.util.UUID.randomUUID().toString();
        this.xapDir = xapDir;
        this.jsDir = jsDir;
    }

    /**
     * 获取ID值
     * @return
     */
    public String getID() {
        return this.id;
    }

    /**
     * 获取xap包路径
     * @return
     */
    public String getXapDir() {
        return this.xapDir;
    }

    /**
     * 设置xap包路径
     * @param xapDir
     */
    public void setXapDir(String xapDir) {
        this.xapDir = xapDir;
    }

    /**
     * 获取js文件路径
     * @return
     */
    public String getJsDir() {
        return this.jsDir;
    }

    /**
     * 设置js文件路径
     * @param jsDir
     */
    public void setJsDir(String jsDir) {
        this.jsDir = jsDir;
    }

    /**
     * 判断是否启用3d
     * @return
     */
    public boolean getView3D() {
        return this.view3D;
    }

    /**
     * 设置是否启用3d
     * @param view3D
     */
    public void setView3D(boolean view3D) {
        this.view3D = view3D;
    }

    /**
     * 宽度
     * @return
     */
    public Double getWidth() {
        return this.width;
    }

    /**
     * 设置宽度
     * @param width
     */
    public void setWidth(Double width) {
        this.width = width;
    }

    /**
     * 高度
     * @return
     */
    public Double getHeight() {
        return this.height;
    }

    /**
     * 设置高度
     * @param height
     */
    public void setHeight(Double height) {
        this.height = height;
    }

    /**
     * 是否启用动态效果
     * @return
     */
    public boolean getAnimationEnabled() {
        return this.animationEnabled;
    }

    /**
     * 设置是否启用动态效果
     * @param animationEnabled
     */
    public void setAnimationEnabled(boolean animationEnabled) {
        this.animationEnabled = animationEnabled;
    }

    /**
     * Theme值
     * @return
     */
    public String getTheme() {
        return this.theme;
    }

    /**
     * 设置皮肤
     * @param theme
     */
    public void setTheme(String theme) {
        this.theme = theme;
    }

    /**
     * 是否使用斜角
     * @return
     */
    public boolean getBevel() {
        return this.bevel;
    }

    /**
     * 设置是否使用斜角
     * @param bevel
     */
    public void setBevel(boolean bevel) {
        this.bevel = bevel;
    }

    /**
     * X轴
     * @return
     */
    public Axis getAxesX() {
        if (this.axesX != null) {
            return this.axesX[0];
            
        } else {
            return null;
            
        }
    }

    /**
     * 设置X轴
     * @param axesX
     */
    public void setAxesX(Axis axesX) {
        if (this.axesX == null) {
            this.axesX = new Axis[1];
        }
        this.axesX[0] = axesX;
    }

    /**
     * Y轴
     * @return
     */
    public Axis getAxesY() {
        if (this.axesY != null) {
            return this.axesY[0];
            
        } else {
            return null;
            
        }
    }

    /**
     * 设置Y轴
     * @param axesY
     */
    public void setAxesY(Axis axesY) {
        if (this.axesY == null) {
            this.axesY = new Axis[1];

            
        }
        this.axesY[0] = axesY;
    }

    /**
     * 是否启用光照效果
     * @return
     */
    public Boolean getLightingEnabled() {
        return this.lightingEnabled;
    }

    /**
     * 设置是否启用光照效果
     * @param lightingEnabled
     */
    public void setLightingEnabled(Boolean lightingEnabled) {
        this.lightingEnabled = lightingEnabled;
    }

    /**
     * 是否启用提示文本
     * @return
     */
    public boolean getToolTipEnabled() {
        return this.toolTipEnabled;
    }

    /**
     * 设置是否启用提示文本
     * @param toolTipEnabled
     */
    public void setToolTipEnabled(boolean toolTipEnabled) {
        this.toolTipEnabled = toolTipEnabled;
    }

    /**
     * 添加数据序列
     * @param series
     */
    public void addSeries(DataSeries series) {
        if (!this.series.isEmpty()) {
            for (DataSeries ds : this.series) {
                Boolean iscompatible = Utility.IsCompatible(ds.getRenderAs(), series.getRenderAs());
                if (!iscompatible) {
                    return;   // 退出执行
                }
            }
        }
        this.series.add(series);
    }

    /**
     * 移除数据序列
     * @param series
     */
    public void removeSeries(DataSeries series) {
        this.series.remove(series);
    }

    /**
     * 添加标题
     * @param title
     */
    public void addTitle(Title title) {
        if (this.titles == null) {
            this.titles = new ArrayList<Title>();

            
        }
        this.titles.add(title);
    }

    /**
     * 移除一个标题
     * @param title
     */
    public void removeTitle(Title title) {
        if (this.titles != null) {
            this.titles.remove(title);
            
        }
    }

    @Override
    public String toString() {
        XStream xstream = new XStream(new XppDomDriver());
        xstream.autodetectAnnotations(true);
        StringWriter sw = new StringWriter();
        xstream.marshal(this, new CompactWriter(sw));
        return sw.toString();
    }
}
